<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Configs;
use App\Models\Demands;
use App\Models\Users;
use App\Models\rewardOrder;
use App\Models\Test;
use App\Models\BalanceLogs;
use App\Http\Requests\Demand;
use Log;
class inspire extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle(){
       //每十分钟执行一次
       
    	$data = [
    		'addtime' => time()
    	];
    	Test::create($data);
    	//1、查询任务是否结束
    	$data['status'] = 4;
    	$map['end_at'] = ['>=',data('Y-m-d H:i:s',time())];
    	Demand::where($map)->update($data);
    	//任务超时
    	$map=[
    		'end_time' => ['>=',data('Y-m-d H:i:s',time())],
    		'status' => 1	
    	];
    	rewardOrder::where($map)->update(array("status"=>5));
    	//自动审核
    	$map=[
    		'status_end_time' => ['>=',time()],
    		'status' => 2,
    		'check_status'=>0
    	];    	
    	$lists = rewardOrder::where($map)->get();
    	foreach ($lists as $key=>$val){
    		$reward = Demand::where("id",$val['reward_id'])->first();
    		//审核时，已完成数达到任务总数，直接视为失败
    		if($reward['ownnumber']>=$reward['number']){
    			$order['status'] = 4;
    			$order['check_status'] = 2;
    			$info = rewardOrder::findOrFail($val['id']);
    			$info->update($order);
    			$content = '您接受的任务"' .$reward['title'] .'"审核失败，其它赏金猎人先于您一步完成，请下次加油';
    		}else{
    			 
    			$order['status'] = 3;
    			$order['check_status'] = 1;
    			$info = rewardOrder::findOrFail($val['id']);
    			$info->update($order);
    			//新增余额
    			Users::where('id',$val['user_id'])->increment('balance',$val['get_money']);
    			//成功数+1
    			Demands::where('id',$val['reward_id'])->increment('ownnumber');
    			$content = '您接受的任务"' .$reward['title'] .'"审核通过，赏金增加'.$val['get_money'];
    			 
    			$user_info = Users::where('id',$val['user_id'])->where('status','<>',-1)->first(['true_name','nickname']);
    			$balance = [
    					'user_id' => $val['user_id'],
    					'user_name' => $user_info['true_name']?$user_info['true_name']:$user_info['nickname'],
    					'money' => $val['get_money'],
    					'balance' => '+'.$val['get_money'],
    						
    					'desc' => '任务收益',
    					'type' => 1,
    			];
    			$balance_info = BalanceLogs::create($balance);
    		}
    	}
    	
    }
}
